package com.student;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import java.util.List;
import com.utils.DaoFactory;
import com.bean.Student;

/**
 * Servlet implementation class StudentLogin
 */
@WebServlet("/StudentLogin")
public class StudentLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		// 获取用户名和密码
		String stu_id = request.getParameter("student_id");
		String stu_pwd = request.getParameter("student_pwd");
		//获取考试信息
		String stu_exam = (String) request.getSession().getAttribute("examname");
		//读取ip地址
		String ip = request.getHeader("x-forwarded-for");
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("X-Real-IP");
		}
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		System.out.println(ip);
		boolean flag=true;
		List<Student> list = DaoFactory.getStudentDaoInstance().allstudentsearch();
		for (Student stu : list) {
			if (stu.getStu_id().equals(stu_id)) {
				if (stu.getStu_ip() != null && !stu.getStu_ip().equals(ip))
					flag = false;
			} else {
				if (stu.getStu_ip()==ip)
					flag = false;
			}
		}
		System.out.println(flag);
		try {
			boolean login = DaoFactory.getStudentDaoInstance().login(stu_id, stu_pwd);
			System.out.println(login);
			if (login && flag) {
				Student student = DaoFactory.getStudentDaoInstance().singlestudentsearch(stu_id);
				//判断有无考试进行
				if (student.getStu_exam().equals(stu_exam)) {
					student.setStu_ip(ip);
					System.out.println(ip);
					int result = DaoFactory.getStudentDaoInstance().update(student, stu_id);
					Student studentname=DaoFactory.getStudentDaoInstance().singlestudentsearch(stu_id);
					if (result > 0) {
						//进入考试界面
						HttpSession session = request.getSession();
						session.setAttribute("stu_name", studentname.getStu_name());
						session.setAttribute("stu_id", stu_id);
						session.setAttribute("stu_pwd", stu_pwd);
						response.sendRedirect("student/show_info.jsp");
					}
				} else {//无考试不让登陆
					response.sendRedirect("index.jsp");
				}
			} else {
				response.sendRedirect("index.jsp");
			}
		} catch (Exception e) {
			// TODO: handle exception
			response.sendRedirect("index.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
